using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace WL.Think.Abp.EntityFrameworkCore
{
    [ConnectionStringName(ThinkConsts.DefaultConnectionStringName)]
    public abstract class AppDbContextBase<TDbContext> : AbpDbContext<TDbContext> where TDbContext : AppDbContextBase<TDbContext>
    {
        protected AppDbContextBase(DbContextOptions<TDbContext> options) : base(options)
        {
        }

        protected sealed override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            ConfigureOptionBuilder(optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }


        /// <summary>
        /// 重写，配置Abp默认的映射关系
        /// </summary>
        /// <param name="builder"></param>
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            ConfigureMapping(builder);
        }

        /// <summary>
        /// 配置DbContext选项构建器
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected virtual void ConfigureOptionBuilder(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.LogTo(Console.WriteLine, LogLevel.Warning);
        }

        /// <summary>
        /// 配置项目映射关系
        /// </summary>
        /// <param name="builder"></param>
        protected abstract void ConfigureMapping(ModelBuilder builder);
    }
}
